<?php
/**
 * Created by PhpStorm.
 * User: CFH
 * Date: 2017/3/27
 * Time: 13:26
 */
class traderate_mdl extends Model{
    public static $table = 'mac_rates';

    public function add_json($item_id,$content,$tags){

        $item = $this->get_one($item_id);
        if(empty($item['item_id'])){
                $this->dbh->insert(self::$table,
                    array(
                        'item_id' => $item_id,
                        'content' => $content,
                        'tags' => $tags
                    )
                );
            $rs_id = $this->dbh->insert_id();
        }else {
            if(empty($item['content']) || $item['content'] == '[]'){
                $this->set_content($item_id,$content);
            }

            if(empty($item['tags']) || $item['tags'] == '[]'){
                $this->set_tags($item_id,$tags);
            }
            $item = $this->get_one($item_id);
        }
        return $item;
    }

    public function set_content($item_id,$content){
        $item = $this->get_one($item_id);
        if(!empty($item['item_id'])){
            $this->dbh->update(self::$table,
                array(
                    'content' => $content
                ),' item_id = '.$item_id
            );
            $rs_id = $this->dbh->insert_id();
            return $rs_id ;
        }
    }


    public function set_tags($item_id,$tags){
        $item = $this->get_one($item_id);
        if(!empty($item['item_id'])){
            $this->dbh->update(self::$table,
                array(
                    'tags' => $tags
                ),'item_id = '.$item_id
            );
            $rs_id = $this->dbh->insert_id();
            return $rs_id ;
        }
    }


    public function get_one($item_id){
        $r = '';
        if(!empty($item_id)){
            $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE item_id = '.$item_id;
            $r = $this->dbh->get_one($sql);
        }
        return $r;
    }

    //删除不存在商品的评价
    public function del_rates_ids($del_ids){
        $result = '';
        foreach ($del_ids as $id){
            $result = $this->dbh->delete(self::$table," item_id = ".$id);
        }
        return $result;
    }

    #region 写数据库
    public function add_json_x($item_id,$content,$tags){
        $item = $this->get_one($item_id);
        if(empty($item['item_id'])){
            $this->dbh->insert(self::$table,
                array(
                    'item_id' => $item_id,
                    'content' => $content,
                    'tags' => $tags
                )
            );
        }
//        $rs_id = $this->dbh->insert_id();
    }
    #endregion

    #region 从数据库中提取 tags和content
    public function get_rates($item_id  = 0, $field = '*', $pageNo = "1", $pageSize = "10"){
        $rs = '';
        if($item_id){
            $sql = 'SELECT ' . $field .
            ' FROM ' . self::$table .
            ' WHERE item_id='.$item_id;
            $rs = $this->get_one($sql);
        }
        return $rs;
                
    }
    #endregion

}